package charpter14.lynx;

import static net.mindview.util.Print.print;

import java.util.HashMap;
import java.util.Map;

interface Interface {
	void doSomething();

	void somethingElse(String arg);
}

class RealObject implements Interface {
	public void doSomething() {
		print("doSomething");
	}

	public void somethingElse(String arg) {
		print("somethingElse " + arg);
	}
}

class SimpleProxy implements Interface {
	private Interface proxied;
	private static Map<String,Integer> counterMap = new HashMap<String, Integer>();

	public SimpleProxy(Interface proxied) {
		this.proxied = proxied;
	}

	public void doSomething() {
		Integer counter = counterMap.get("doSomething");
		if (null == counter){
			counter = 1;
		}else {
			counter++;
		}
		counterMap.put("doSomething", counter);
		
		print("SimpleProxy doSomething " + counter + " times.");
		proxied.doSomething();
	}

	public void somethingElse(String arg) {
		Integer counter = counterMap.get("somethingElse");
		if (null == counter){
			counter = 1;
		}else {
			counter++;
		}
		counterMap.put("doSomething", counter);
		print("SimpleProxy somethingElse " + arg + " " + counter + " times.");
		proxied.somethingElse(arg);
	}
}

public class Twentyone {
	  public static void consumer(Interface iface) {
		    iface.doSomething();
		    iface.somethingElse("bonobo");
		  }
		  public static void main(String[] args) {
		    consumer(new RealObject());
		    consumer(new SimpleProxy(new RealObject()));
		  }
}
